package com.ssm.okex.trade.strategy;

import java.util.ArrayList;
import java.util.Random;

/**
 * 1. 移动平均线策略
 */
public class MovingAverageStrategy {

    // 计算移动平均线
    public static ArrayList<Double> calculateMovingAverage(ArrayList<Double> prices, int period) {
        ArrayList<Double> maValues = new ArrayList<>();
        double sum = 0;
        for (int i = 0; i < prices.size(); i++) {
            sum += prices.get(i);
            if (i >= period) {
                sum -= prices.get(i - period);
                maValues.add(sum / period);
            }
        }
        return maValues;
    }

    // 判断买入/卖出信号
    public static String maSignal(ArrayList<Double> maValues) {
        String signal = "";
        if (maValues.get(maValues.size() - 2) < maValues.get(maValues.size() - 1)) {
            signal = "BUY";
        } else if (maValues.get(maValues.size() - 2) > maValues.get(maValues.size() - 1)) {
            signal = "SELL";
        }
        return signal;
    }

    // 主方法
    public static void main(String[] args) {
        ArrayList<Double> prices = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            prices.add((double) (new Random().nextInt(50) + 50));
        }
//        prices.add(50.0);
//        prices.add(60.0);
//        prices.add(70.0);
//        prices.add(80.0);
//        prices.add(90.0);
//        prices.add(90.0);
//        prices.add(90.0);
//        prices.add(90.0);
//        prices.add(90.0);

        ArrayList<Double> maValues = MovingAverageStrategy.calculateMovingAverage(prices, 3);
        System.out.println("移动平均线值: " + maValues);

        String signal = MovingAverageStrategy.maSignal(maValues);
        System.out.println("买卖信号: " + signal);
    }
}